﻿using CarRental.DDL;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CarRental.View.Order {
    public partial class MakeOrder : System.Web.UI.Page {
        DbAccess _dbAccess = new DbAccess();
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                //EntityDataSource_SearchCar.AutoGenerateWhereClause = true;

                ddlDepot.DataSource = new CarRental.Helpers.OptionItems.DepotLst(true);
                ddlDepot.DataValueField = "Key";
                ddlDepot.DataTextField = "Title";
                ddlDepot.DataBind();

                ddlDrivetrains.DataSource = new CarRental.Helpers.OptionItems.DriveTrainsLst(true);
                ddlDrivetrains.DataValueField = "Key";
                ddlDrivetrains.DataTextField = "Title";
                ddlDrivetrains.DataBind();

                ddlColor.DataSource = new CarRental.Helpers.OptionItems.ColorLst(true);
                ddlColor.DataValueField = "Key";
                ddlColor.DataTextField = "Title";
                ddlColor.DataBind();

                ddlCarType.DataSource = new CarRental.Helpers.OptionItems.CarTypeLst(true);
                ddlCarType.DataValueField = "Key";
                ddlCarType.DataTextField = "Title";
                ddlCarType.DataBind();

                ddlShop.DataSource = new CarRental.Helpers.OptionItems.ShopLst(false);
                ddlShop.DataValueField = "Key";
                ddlShop.DataTextField = "Title";
                ddlShop.DataBind();


            }
        }

        protected void btn_AddCart_Click(object sender, EventArgs e) {
            string _s = "";
            for (int i = 0; i < gvCarLst.Rows.Count; i++) {
                CheckBox chkb = (CheckBox)gvCarLst.Rows[i].Cells[0].FindControl("chk");
                if (chkb.Checked) {
                    _s += gvCarLst.Rows[i].Cells[1].Text;

                }
            }
        }

        protected void btnSearchCar_Click(object sender, EventArgs e) {
            if (string.IsNullOrEmpty(tbRentFDate.Text) ||
                string.IsNullOrEmpty(tbRentTDate.Text)) {
                lblWarning.Text = "Date period missing. Please select the other date.";
            } else {
                DateTime _rentFormDate = DateTime.ParseExact(tbRentFDate.Text, "yyyy-MM-dd", null);
                DateTime _rentToDate = DateTime.ParseExact(tbRentTDate.Text, "yyyy-MM-dd", null);
                if (_rentFormDate < DateTime.Now ||
                    _rentToDate < DateTime.Now ||
                    (_rentFormDate - _rentToDate).TotalDays > 0) {
                    lblWarning.Text = "Wrong date period. Please select the other date.";
                } else {
                    List<vwcar> _vwcar = _dbAccess.GetspqGetCarAllowToRent(_rentFormDate, _rentToDate, ddlShop.SelectedValue);
                    gvCarLst.DataSource = _vwcar;
                    gvCarLst.DataBind();

                    lblWarning.Text = "";
                }
            }
            //EntityDataSource.WhereParameters.Clear();
            //EntityDataSource.AutoGenerateWhereClause = true;
            //EntityDataSource.WhereParameters.Add("user_type", TypeCode.String, ddlUserType.SelectedValue);
        }

        protected void gvCarLst_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "Order") {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvCarLst.Rows[index];
                //int ServerID = Convert.ToInt32(gvUser.DataKeys[index].Value);
                var carId = row.Cells[0].Text;


                Response.Redirect("~/View/Order/ConfirmOrder.aspx?CarId=" + carId
                                                               + "&FromDate=" + tbRentFDate.Text
                                                               + "&ToDate=" + tbRentTDate.Text);
            }
        }
    }
}